//
//  CAChooseIndustryTableViewCell.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/6/15.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "CAChooseIndustryTableViewCell.h"


@interface CAChooseIndustryTableViewCell()

@property (weak, nonatomic) IBOutlet UIView *myView;


@end

@implementation CAChooseIndustryTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.myView.layer setMasksToBounds:YES];
    [self.myView.layer setBorderWidth:0.5];
    [self.myView.layer setBorderColor:[[UIColor colorWithRed:190.0/255.0 green:190.0/255.0 blue:190.0/255.0 alpha:1] CGColor]];
    
    // Initialization code
}

- (void)setModelAry:(NSMutableArray *)modelAry{
    
    if (modelAry != nil) {
        
        _modelAry = modelAry;
        
        //判断是否是4的倍数 也就是说能不能填满一行
        if (modelAry.count % 4 != 0) {
            
            for (int i = 0; i < modelAry.count % 4; i++) {
                Friend *fri = [[Friend alloc] init];
                fri.name = @"";//名字为空 和 请求的数据做对比
                [_modelAry addObject:fri];
            }
            
        }
        
        int column = 4.0;
        
        CGFloat labelW = self.myView.width / column + 0.5 ;
        CGFloat labelH = 35 + 0.5;
        
        for (int i = 0; i < self.modelAry.count; i++) {
            
            int row = i / column;
            int loc = i % column;
            
            CGFloat lx = labelW * loc - (0.5 * loc);
            CGFloat lY = labelH * row - (0.5 * row);
            
            UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(lx, lY, labelW, labelH))];
            label.font = [UIFont systemFontOfSize:12];
            label.textColor = [UIColor grayColor];
            label.text = [self.modelAry[i] name];
            label.textAlignment = NSTextAlignmentCenter;
            label.userInteractionEnabled = YES;
            
            [label.layer setMasksToBounds:YES];
            [label.layer setBorderWidth:0.5];
            [label.layer setBorderColor:[[UIColor colorWithRed:190.0/255.0 green:190.0/255.0 blue:190.0/255.0 alpha:1] CGColor]];
            
            label.tag = i;
            
            if(![[self.modelAry[i] name] isEqualToString:@""]){
                
                [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)]];
            }
            
            [self.myView addSubview:label];
        }
    } 
    
}

- (void)labelClick:(UITapGestureRecognizer *)aTap{
    
    if (self.chooseBlock != nil) {
        
        self.chooseBlock(self.modelAry[aTap.view.tag]);
    } 
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
